<?php

namespace app\controller;

use think\Request;
use app\service\NotificationService;
use Exception;

class Notification
{
    protected $notificationService;

    public function __construct()
    {
        $this->notificationService = new NotificationService();
    }

    /**
     * 获取用户通知列表
     */
    public function index(Request $request)
    {
        try {
            $userId = $request->userId;

            $notifications = $this->notificationService->getUserNotifications($userId);

            return success($notifications, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取通知统计
     */
    public function stats(Request $request)
    {
        try {
            $userId = $request->userId;

            $stats = $this->notificationService->getNotificationStats($userId);

            return success($stats, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取即将过期的密码通知
     */
    public function expiring(Request $request)
    {
        try {
            $userId = $request->userId;
            $days = $request->param('days', 7);

            $notifications = $this->notificationService->getExpiringPasswordNotifications($userId, $days);

            return success($notifications, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取已过期的密码通知
     */
    public function expired(Request $request)
    {
        try {
            $userId = $request->userId;

            $notifications = $this->notificationService->getExpiredPasswordNotifications($userId);

            return success($notifications, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 批量设置密码过期时间
     */
    public function batchSetExpiration(Request $request)
    {
        try {
            $userId = $request->userId;
            $accountIds = $request->param('account_ids');
            $expiresAt = $request->param('expires_at');

            if (empty($accountIds) || !is_array($accountIds)) {
                return error('请选择要设置的账号');
            }

            if (empty($expiresAt)) {
                return error('请选择过期时间');
            }

            $count = $this->notificationService->batchUpdateExpirationDate($userId, $accountIds, $expiresAt);

            return success(['count' => $count], "成功设置{$count}个账号");

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 设置默认密码过期时间
     */
    public function setDefaultExpiration(Request $request)
    {
        try {
            $userId = $request->userId;
            $accountId = $request->param('account_id');
            $days = $request->param('days');

            if (empty($accountId)) {
                return error('账号ID不能为空');
            }

            $account = $this->notificationService->setDefaultPasswordExpiration($userId, $accountId, $days);

            return success($account, '设置成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }
}
